/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Business.Cart;
import Business.Customer;
import Business.LineItem;
import Business.Order;
import com.google.code.kaptcha.Constants;
import java.io.IOException;
//import java.sql.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 *
 * @author Nguyen Hung
 */
public class PaymentServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String url = "";
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        String phoneNumber = request.getParameter("phonenumber");
        String paytype = request.getParameter("paytype");
        String note = request.getParameter("note");
        request.setCharacterEncoding("UTF-8");
        String kaptchaExpected = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        String kaptchaReceived = request.getParameter("kaptcha");
        
        
        
        HttpSession session = request.getSession();
        
        
        session.setAttribute("address", address);
        session.setAttribute("phonenumber", phoneNumber);
        
        

        if (kaptchaExpected.equalsIgnoreCase(kaptchaReceived)) {
            
            Customer customer = new Customer(name, email, address, phoneNumber);
            session.setAttribute("customer", customer);
//            request.setAttribute("customer", customer);
//            request.setAttribute("note", note);
            response.setContentType("text/html; charset=UTF-8");
            Cart cart = (Cart) session.getAttribute("cart");
            if (cart == null || cart.getCount() == 0) {

                url = "/error.jsp";
            } else {

                Date date = new Date();
                
                Order order = new Order( date, customer, note, paytype, null);
                            
//            order.addAll(cart.getItems());
                order.setLineItems(cart.getItems());
                
//                order.save();
                
            
                
                if(paytype.equals("Chuyển Khoản Ngân Lượng")){
                    url = "https://www.nganluong.vn/button_payment.php?receiver=dalathoanghon08@gmail.com&product_name=" + order.getIdOrder()+"&price="+10000+"&return_url=summary.jsp&comments="+order.getNote();
                    order.setStatus("Thanh Toán");
                    response.setCharacterEncoding("utf-8");
                    response.sendRedirect(url);
                }
                else{
                    order.setStatus("Chưa Thanh Toán");
                    url = "/summary.jsp";
                }
                
                session.setAttribute("order", order);
            }

        } else {
            url = "/payment.jsp";
            request.setAttribute("error", "Nhập lại mã bảo vệ");
        }

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);





    }

  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

   
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
